package com.linksky.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linksky.dao.SongRepository;
import com.linksky.entity.Song;


@Controller
public class SongController {
	@Autowired
	private SongRepository SongRepository;

	/**
	 * 歌曲分页查询
	 * @return
	 */
	@RequestMapping("/index")
	public String getSong(ModelMap map,@RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value ="count", defaultValue ="20") Integer size ) {
		Sort sort = new Sort(Direction.DESC, "id");
		System.out.println(page);
		System.out.println(size);
	    Pageable pageable = new PageRequest(page,size, sort);
		Page<Song> songList= SongRepository.findAll(pageable);
		map.put("list", songList);
		return "index";
	}
	/**
	 * 歌曲分页查询
	 * @return
	 */
	@RequestMapping("/song")
	@ResponseBody
	public Page<Song> Song(ModelMap map,@RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value ="count", defaultValue ="20") Integer size ) {
		Sort sort = new Sort(Direction.DESC, "id");
		System.out.println(page);
		System.out.println(size);
	    Pageable pageable = new PageRequest(page,size, sort);
		Page<Song> songList= SongRepository.findAll(pageable);
		System.out.println(songList);
		return songList;
	}
}
